<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/26
 * Time: 21:44
 */

namespace Home\Controller;


use Home\Common\Controller\LoginedController;

/**
 * Class OrderController
 * @package Home\Controller
 * 用户订单管理
 */
class OrderController extends LoginedController
{
    /**
     * 订单显示页
     */
   public function  index(){
       $uid=session('uid');
       $this->display();
   }
    /**
     * 订单支付页面
     */
    public function pay(){
//        dd(getdate());
       if(IS_AJAX){
            session('uid_goods',I('post.'));
           $this->ajaxReturn(1);
       }
        if(!isset($_GET['paystatus'])){
            $this->redirect('home/car/lists');die;
        }
        $res=$this->pays();
        $zhifu=[
            'goods'=>$res['arr'],
            'zongshu'=>$res['zongshu'],
            'jiage'=>$res['jiage'],
        ];
        $this->assign('zhifu',$zhifu);
//        dd($zhifu);
       //找出该用户的地址
        $uid=session('uid');
        $addr=M('useraddr')->where("uid=$uid")->select();
//        dd($addr);
        $this->assign('addr',$addr);
//        找出默认的地址
        $moren=M('useraddr')->where("uid=$uid AND poststatus=1")->find();
        $this->assign('moren',$moren);
//        dd($moren);
        $this->display();
    }

    /**
     * 计算用户要支付的信息
     *
     */
    private function pays(){
        //查找用户选择付款的商品
        $goods=session('uid_goods');
        $good=[];
        //重组商品组合信息
        foreach($goods['gid'] as $k=>$v){
            $good[]=[
                'gid'=>$v,
                'pcid'=>$goods['pcid'][$k],
            ];
        }
        $arr=[];
        //找出用户选择付款的商品
        foreach($good as $k=>$v){
            $arr[]=M('usergoods')
                ->join("sp_goods ON sp_usergoods.gid=sp_goods.gid")
                ->where("sp_usergoods.gid={$v['gid']} AND sp_usergoods.pcid={$v['pcid']}")
                ->field("sp_goods.gname,sp_goods.thumb,sp_goods.priced,sp_usergoods.*")
                ->find();
        }
        //计算单样商品总价，
        foreach($arr as $k=>$v){
            //追加单样商品总价
            $arr[$k]['zongjia']=$v['priced']*$v['count'];
        }
        //计算付款商品总价，总数量
        $zongshu=0;
        $jiage=0;
        foreach($arr as $k=>$v){
            $zongshu=$v['count']+$zongshu;
            $jiage=$v['zongjia']+$jiage;
        }
        return ['arr'=>$arr,'zongshu'=>$zongshu,'jiage'=>$jiage];
    }
   /**
    * 地址管理
    */
    public function address(){
        if(IS_POST){
            $_POST['uid']=session('uid');
           $res=D('useraddr')->store(I('post.'));
           if($res){
               $this->success('添加成功');
           }else{
               $this->error('添加失败');
           }
        }
        $this->display();
    }

    /**
     * 异步添加地址
     */
    public function ajaxaddaddr(){
        if(IS_AJAX){
            $_POST['uid']=session('uid');
//            dd($_POST);die;
            $res=D('useraddr')->store(I('post.'));
            if($res['vaild']){
                $this->ajaxReturn($res);
            }else{
                $this->ajaxReturn($res);
            }

        }
    }
    /**
     * 结算
     */
    public function  ajaxPay(){
        if(IS_AJAX){
//               dd($_POST);
              $goodinfo=$this->pays();
//            dd($goodinfo);die;
            //查询用户选择的地址
            $addr=M('useraddr')->where("udid={$_POST['udid']}")->find();
            //向用户订单表插入数据
            $data['postname']=$addr['postname'];
            $data['postaddr']=$addr['useraddr'];
            $data['orderpay']=$goodinfo['jiage'];
            $data['phone']=$addr['phone'];
            $data['postcode']=$addr['postcode'];
            $data['uid']=session('uid');
            $data['ordertime']=time();
            if($_POST['voi']=='1'){
                $data['invoice']=$_POST['danweiname'];
            }else{
                $data['invoice']='不开发票';
            }
            $uoid=D('userorder')->addorder($data);
//            dd($uoid);die;
            //想用户已支付订单表中插入数据
//            生成订单号
            $ordernumber=getdate()['year'].getdate()['mon'].getdate()['mday'].$uoid;
            $orderpay['ordernumber']=$ordernumber;
            $orderpay['paymoney']=$goodinfo['jiage'];
            $orderpay['count']=$goodinfo['zongshu'];
            $orderpay['paytime']=$data['ordertime'];
            $orderpay['uoid']=$uoid;
            $orderpay['uid']=session('uid');
            //订单详情
            $orderpay['orderinfo']=json_encode($goodinfo['arr'],JSON_UNESCAPED_UNICODE);
            $id=D('payorder')->addpayorder($orderpay);
            //更改用户购无车表中的商品状态
            $ugids=[];
            foreach($goodinfo['arr'] as $k=>$v){
              $ugids[]=$v['ugid'];
            }
            $map['uid']=$orderpay['uid'];
            $map['ugid']=['in',$ugids];
            $resrows=M('usergoods')->where($map)->setField('gstatus',1);
            //更改货品表中的库存
//            dd($goodinfo);die;
            foreach($goodinfo['arr'] as $k=>$v){
                $pcid=$v['pcid'];
                $count=$v['count'];
                 $descrow=M('product')->where("pcid={$pcid}")->setDec('stock',$count);
            }
            if($descrow){
                session('uid_goods',null);
                $this->ajaxReturn(['valid'=>1,'ordernumber'=>$ordernumber]);die;
            }else{
                $this->ajaxReturn(['valid'=>0,'ordernumber'=>'异常']);die;
            }
//              dd(session());

//            生成订单号


        }
    }
}